<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mtrans extends CI_Model {
  public function __construct()
  {
    parent::__construct();
    $this->load->library('serversidedatatables');   
  }

  public function sell_in_sales_team( $cluster_id, $user_group ) {
    $this->db->distinct();
    $this->db->select('uu.user_id, initcap(uu.user_name) as user_name, uu.user_group_id, gg.user_group_caption');
    $this->db->join('t_mtr_user_group AS gg', 'uu.user_group_id = gg.user_group_id AND uu.istatus = 1 AND gg.having_stock = 1');
    $this->db->join('t_trx_scan_in AS ss', 'uu.user_id = ss.user_id');
    $this->db->join('t_trx_scan_in_detail AS sd', 'sd.scan_in_id = ss.scan_in_id AND sd.istatus = 1');
    $this->db->where("f_get_cluster(uu.user_id)", $cluster_id);
    $this->db->where_in("gg.user_group_id", $user_group);
    $this->db->order_by("uu.user_group_id, user_name", "asc");
    $query = $this->db->get("t_mtr_user AS uu");
    return $query->result();
  }

  public function get_partners($user_group_id, $sales_id) {
    $this->db->select("channel_id, f_get_channel_name(channel_id) AS channel_name");
    $this->db->where("user_id", $sales_id);
    $this->db->where("yyyymm", date("Ym"));
    $this->db->where("d".date('d'), 1 );
    $query = $this->db->get("t_jc_$user_group_id");
    return $query->result();
  }

  public function get_program( $user_group_id, $channel_id ) {
    if ( $user_group_id == 8 ) { $program_type = array(1,3) ;} elseif ( $user_group_id == 9) { $program_type = array(2,4) ;}

    $this->db->select("parent_id");
    $this->db->where("territory_id", "f_get_territory_id_by_channel_id('$channel_id')", false );
    $cluster_id = $this->db->get("t_mtr_territory")->row()->parent_id;

    $this->db->select("program_id, caption, description, territory_id");
    $this->db->where_in("program_type_id", $program_type );
    $this->db->where_in("territory_id", array(1, $cluster_id) );
    $this->db->where("istatus", 1);
    $query = $this->db->get("t_mtr_program");
    return $query->result();
  }

}

/* End of file mtrans.php */
/* Location: ./application/models/mtrans.php */